package com.mingchuang.administrator.mingchuang.project;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.mingchuang.administrator.mingchuang.AppApplication;
import com.mingchuang.administrator.mingchuang.R;
import com.mingchuang.administrator.mingchuang.bean.ProjectBean;
import com.mingchuang.administrator.mingchuang.pile.PileListActivity;

import java.util.List;

/**
 * Created by kipeng on 2016-3-23.
 */
public class ProjectActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ListView listView;
    private View project_btn;
    private View pile_btn;
    private Button upload_btn;

    private List<ProjectBean> uploadList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_page_layout);


        initView();
    }

    private void initView() {

        Log.d("kipeng","hello project");

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("工程列表");

        listView = (ListView) findViewById(R.id.list_content);


        //查询project list

        DbUtils db = AppApplication.getDb();
        try {
            uploadList = db.findAll(ProjectBean.class);
            ProjectAdapter adapter = new ProjectAdapter(this , uploadList);
            listView.setAdapter(adapter);
        } catch (DbException e) {
            e.printStackTrace();
        }



        listView.setOnItemClickListener(this);

        project_btn = findViewById(R.id.project_btn);
        project_btn.setOnClickListener(this);
        pile_btn = findViewById(R.id.pile_btn);
        pile_btn.setOnClickListener(this);
        upload_btn = (Button) findViewById(R.id.upload_btn);
        upload_btn.setOnClickListener(this);
        project_btn.setVisibility(View.GONE);
        pile_btn.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.project_btn://工程列表
                Intent intent = new Intent();
                intent.setClass(this, ProjectActivity.class);
                startActivity(intent);


                break;

            case R.id.pile_btn://基桩列表


                break;
            case R.id.upload_btn:

                break;
            default:

                break;
        }


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent();
        intent.setClass(this,PileListActivity.class);
        intent.putExtra("name",uploadList.get(position).getName());

        startActivity(intent);


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);


    }
}
